package com.ruoyi.quartz.task;

import com.ruoyi.adrss.Http;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.management.carservicestatics.service.ITCarServiceCollectService;
import com.ruoyi.management.carservicestatics.service.ITCarServiceStaticsService;
import com.ruoyi.passenger.order.service.ITOrderService;
import com.ruoyi.passenger.order.vo.GetOrderLocationVO;
import com.ruoyi.system.service.ISysConfigService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 根据经纬度获取地址
 * 
 * @author lss
 */
@Component("addressTask")
public class AddressTask
{

    @Autowired
    private ITOrderService tOrderService;
    @Autowired
    private ISysConfigService configService;

    /**
     * 同步车辆营运信息到订单
     */
    @Transactional(rollbackFor = Exception.class)
    public void address() {
        //获取未同步行程信息的订单
        List<GetOrderLocationVO> addressList = tOrderService.getAddress();
        for (GetOrderLocationVO address : addressList) {
            String startLocation = address.getStartCoordinate();
            String endLocation = address.getEndCoordinate();
            String[] starts = startLocation.split(",");
            StringBuilder startBuilder = new StringBuilder();
            startBuilder.append(starts[1]);
            startBuilder.append(",");
            startBuilder.append(starts[0]);
            startLocation = startBuilder.toString();
            //下车经纬度拼接
            String[] ends = endLocation.split(",");
            StringBuilder endBuilder = new StringBuilder();
            endBuilder.append(ends[1]);
            endBuilder.append(",");
            endBuilder.append(ends[0]);
            endLocation = endBuilder.toString();
            //获取调用百度地图接口密钥ak
            String ak = configService.selectConfigByKey("ak");
            if (StringUtils.isEmpty(ak)) {
                continue;
            }
            //调用百度地图接口
            StringBuilder startUrl = new StringBuilder();
            startUrl.append("https://api.map.baidu.com/reverse_geocoding/v3/?ak=");
            startUrl.append(ak);
            startUrl.append("&output=json&coordtype=wgs84ll&extensions_town=true&location=");
            startUrl.append(startLocation);
            address.setStartCoordinate(Http.doGet(startUrl.toString()));
            StringBuilder endUrl = new StringBuilder();
            endUrl.append("https://api.map.baidu.com/reverse_geocoding/v3/?ak=");
            endUrl.append(ak);
            endUrl.append("&output=json&coordtype=wgs84ll&extensions_town=true&location=");
            endUrl.append(endLocation);
            address.setEndCoordinate(Http.doGet(endUrl.toString()));
        }
        if (!CollectionUtils.isEmpty(addressList)) {
            tOrderService.updateOrderAddressList(addressList);
        }
    }
}
